package servletsAction;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Cadastro;
import dao.Conexao;
import dao.ContatoDAO;

/**
 * Servlet implementation class UpdateContato
 */
public class UpdateContato extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateContato() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String nome = request.getParameter("nome");
		 String  sobrenome=request.getParameter("sobrenome");
		 String  telefone =request.getParameter("telefone") ;
		 String   celular =request.getParameter("celular");
		 String   email =request.getParameter("email");
		 String   cep = request.getParameter("cep");
		 String  rua = request.getParameter("rua") ;
		 String   numero= request.getParameter("numero");
	     String   bairro =request.getParameter("bairro");
		 String  complemento=request.getParameter("complemento");
		 String idContato = request.getParameter("id");
		 Cadastro cadastro = new Cadastro();
		 Conexao concta =  new Conexao();
		 Connection conexao = concta.open();
		 ContatoDAO contatoDAO = new ContatoDAO(conexao);
		 if(nome.trim().equals("")|| sobrenome.trim().equals("")||telefone.trim().equals("")||celular.trim().equals("")||email.trim().equals("")||cep.trim().equals("")||
				 rua.trim().equals("")||numero.trim().equals("")||bairro.trim().equals("")){
			 cadastro = null;
			 RequestDispatcher view = request.getRequestDispatcher("/erroCadastro.html");
		        
		        view.forward(request, response);
		        return;
		 }
		 
		 cadastro.setIdCadastro(idContato);
		 cadastro.setBairro(bairro);
		 cadastro.setCelular(celular);
		 cadastro.setCep(cep);
		 cadastro.setComplemento(complemento);
		 cadastro.setEmail(email);
		 cadastro.setNome(nome);
		 cadastro.setNumero(numero);
		 cadastro.setRua(rua);
		 cadastro.setSobrenome(sobrenome);
		 cadastro.setTelefone(telefone);
		 
		 contatoDAO.update(cadastro);
		 request.getSession().setAttribute("bean", cadastro);
		
	        RequestDispatcher view = request.getRequestDispatcher("/home.html");
	        
	        view.forward(request, response);
	}

}
